# 2024/4/8 下午8:08 HFL
import traceback
from time import sleep

import allure
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from Financial_operation.create_phone import create_name, phone
from Financial_operation.open_an_account_Page import open_an_account_Page
from api.test_01.demo06 import generate_random_id


class TestOpenAccount:

    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def teardown_class(self):
        self.driver.quit()
        # 注册成功

    @allure.title('开通资金托管账户')
    @allure.severity(allure.severity_level.CRITICAL)
    def test01_reg(self):
        with allure.step('打开页面url'):
            driver = self.driver
            driver.get('http://121.43.169.97:8081/common/member/reg')
        with allure.step('输入用户名'):
            driver.find_element(By.ID, 'phone').send_keys(phone())
        with allure.step('输入密码'):
            driver.find_element(By.ID, 'password').send_keys('a123456')
        with allure.step('输入验证码'):
            driver.find_element(By.ID, 'verifycode').send_keys('8888')
        with allure.step('点击获取短信验证码'):
            driver.find_element(By.ID, 'get_phone_code').click()
        sleep(1)
        with allure.step('输入短信验证码'):
            driver.find_element(By.ID, 'phone_code').send_keys('666666')
        sleep(1)
        with allure.step('点击注册'):
            driver.find_element(By.CSS_SELECTOR, '[value="免费注册"]').click()
        sleep(1)
        with allure.step('点击下一步'):
            driver.find_element(By.XPATH, '//*[@id="step3"]/div/div/div/div/a').click()
        sleep(2)
        with allure.step('输入姓名'):
            driver.find_element(By.CSS_SELECTOR, '[placeholder="请输入姓名"]').send_keys(create_name())
        with allure.step('输入身份证号码'):
            driver.find_element(By.CSS_SELECTOR, '[placeholder="请输入身份证号码"]').send_keys(generate_random_id())
        with allure.step('点击提交'):
            driver.find_element(By.CSS_SELECTOR, '[value="确认提交"]').click()
        sleep(2)
        with allure.step('点击开通'):
            driver.find_element(By.CSS_SELECTOR, '[value="立即开通"]').click()
        sleep(2)
        with allure.step('获取当前窗口的句柄'):
            driver.switch_to.window(driver.window_handles[-1])
        with allure.step('断言'):
            try:

                res = driver.find_element(By.XPATH, '/html/body').text
                assert res == 'UserRegister OK'
            except:
                traceback.print_exc()
                allure.attach(driver.get_screenshot_as_png(), '开户失败截图', allure.attachment_type.PNG)
                pytest.fail('断言失败')
        sleep(1)

    # def test02_reg(self):
    #     driver = webdriver.Chrome()
    #     driver.maximize_window()
    #     driver.implicitly_wait(10)
    #     username = '13180577872'
    #     pwd = 'a123456'
    #     driver.get('http://121.43.169.97:8081/common/member/login')
    #     driver.find_element(By.ID, 'keywords').send_keys(username)
    #     driver.find_element(By.ID, 'password').send_keys(pwd)
    #     driver.find_element(By.ID, 'login-btn').click()
    #     sleep(1)
    #     # 点击借款账户
    #     driver.find_element(By.XPATH, '//*[@id="mlayout"]/div[2]/div[2]/div[1]/a/em').click()
    #     # 点击申请额度
    #     driver.find_element(By.XPATH, "//*[text()='申请额度']").click()
    #     # 填写申请额度信息
    #     driver.find_element(By.ID, 'amount_account').send_keys('99999')
    #     sleep(0.5)
    #     driver.find_element(By.XPATH, '//*[@id="mamountapply"]/div[3]/textarea').send_keys('原神启动')
    #     sleep(0.5)
    #     driver.find_element(By.ID, 'verifycode').send_keys('8888')
    #     driver.find_element(By.CSS_SELECTOR, '[value="确认提交"]').click()
    #     sleep(3)
    #     driver.get('http://121.43.169.97:8082/')
    #     driver.find_element(By.ID, 'username').send_keys('admin')
    #     driver.find_element(By.ID, 'password').send_keys('HM_2023_test')
    #     driver.find_element(By.ID, 'valicode').send_keys('8888')
    #     driver.find_element(By.CSS_SELECTOR, '[value="登录"]').click()
    #     sleep(1)
    #     driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/ul/li[2]/a').click()
    #     driver.find_element(By.XPATH, '//*[@id="sidebar"]/ul/li[5]/a/span').click()
    #     driver.find_element(By.XPATH, '//*[@id="sidebar"]/ul/li[5]/ul/li[2]/a').click()
    #     driver.switch_to.frame('iframe_box')
    #     driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/ul/li[1]/div/input').send_keys(username)
    #     sleep(0.5)
    #     Select(driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/ul/li[2]/div/select')).select_by_value('0')
    #     sleep(0.5)
    #     driver.find_element(By.CSS_SELECTOR, '[value="搜索"]').click()
    #     res3 = driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/table/tbody/tr/td[2]/span').text
    #     if res3 == username:
    #         sleep(2)
    #         driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/table/tbody/tr[1]/td[2]').click()
    #         driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/ul/li[1]/a/span').click()
    #         driver.switch_to.frame('xubox_iframe1')
    #         driver.find_element(By.XPATH, '/html/body/div[2]/form/table/tbody/tr[5]/td[2]/div/label[1]/span').click()
    #         driver.find_element(By.XPATH, '/html/body/div[2]/form/table/tbody/tr[6]/td[2]/div/textarea').send_keys(
    #             '111')
    #         driver.find_element(By.XPATH,
    #                             '/html/body/div[2]/form/table/tbody/tr[7]/td[2]/div/ul/li[1]/input').send_keys('8888')
    #         driver.find_element(By.XPATH, '/html/body/div[2]/form/table/tbody/tr[8]/td[2]/input[1]').click()
    #         driver.switch_to.default_content()
    #         driver.switch_to.frame('iframe_box')
    #         driver.switch_to.frame('xubox_iframe1')
    #         sleep(1)
    #         res4 = driver.find_element(By.XPATH, '//*[@id="xubox_layer1"]/div[1]/div/span[2]').text
    #         sleep(1)
    #         assert res4 == '审核成功'
    #         print('审核成功')
    #     else:
    #         print('审核失败')
    #     driver.get('http://121.43.169.97:8081/common/member/login')
    #     driver.find_element(By.ID, 'keywords').send_keys(username)
    #     driver.find_element(By.ID, 'password').send_keys(pwd)
    #     driver.find_element(By.ID, 'login-btn').click()
    #     sleep(1)
    #     # 点击借款账户
    #     driver.find_element(By.XPATH, '//*[@id="mlayout"]/div[2]/div[2]/div[1]/a/em').click()
    #     # 点击申请额度
    #     driver.find_element(By.XPATH, "//*[text()='申请额度']").click()
    #     res = driver.find_elements(By.XPATH, '//*[@id="amount_list"]/tr[1]/td[6]')[0].text
    #     assert res == '审核通过'
    #     sleep(2)
    #     driver.quit()
    #
    # def test03_reg(self):
    #     op = open_an_account_Page()
    #     op.open_an_account()
